//树形dp - 时态同步
#include <iostream>
#include <vector>
using namespace std;
const int N = 5e5 + 10;
int n, s;
typedef pair<int, int> PII;
typedef long long ll;
vector<PII> edges[N];
ll ret;
ll f[N]; //f[x]表示以x为根的树, 从根节点到叶节点的最长路径和

void dfs(int x, int fa)
{
	for (auto t : edges[x])
	{
		int y = t.first, z = t.second;
		if (y == fa) continue;
		dfs(y, x);
		f[x] = max(f[x], f[y] + z);
	}

	for (auto t : edges[x])
	{
		int y = t.first, z = t.second;
		if (y == fa) continue;
		ret += f[x] - (z + f[y]);
	}
}

int main()
{
	cin >> n >> s;
	for (int i = 1; i < n; i++)
	{
		int a, b, t; cin >> a >> b >> t;
		edges[a].push_back({ b, t });
		edges[b].push_back({ a, t });
	}
	dfs(s, 0);
	cout << ret << endl;
	return 0;
}